<?php

namespace App\Http\Requests\Wechat;

use App\Models\Wechat;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class CreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $wechat = Wechat::first();
        $rules = [
            'app_id'  => [
                'required'
            ],
            'secret'  => [
                'required'
            ],
            'mch_id'  => [
                'required'
            ],
            'mch_key' => [
                'required'
            ]
        ];
        if (is_null($wechat)) {
            $ca = [
                'ca_cert'        => [
                    'required',
                    'file'
                ],
                'apiclient_key'  => [
                    'required',
                    'file'
                ],
                'apiclient_cert' => [
                    'required',
                    'file'
                ]
            ];
        } else {
            $ca = [
                'ca_cert'        => [
                    'nullable',
                    'file'
                ],
                'apiclient_key'  => [
                    'nullable',
                    'file'
                ],
                'apiclient_cert' => [
                    'nullable',
                    'file'
                ]
            ];
        }
        return array_merge($rules, $ca);
    }
}
